<?php

  // get the form inputs
  $nombre = get_input('nombre');
  $descripcion = get_input('descripcion');
  $acceso = get_input('acceso');
  $grado = get_input('grado');
  $sector_guid = get_input('sector_guid');
  
    // crear un nuevo objeto via
  $via = new ElggObject();
  $via->subtype = "via";
  $via->title = $nombre;
  $via->description = $descripcion;
  
  $via->access_id = ACCESS_PUBLIC;
 
  // owner is logged in user
  $via->owner_guid = elgg_get_logged_in_user_guid();
 
  // save tags as metadata
 
  // save to database and get id of the new blog
  $via_guid = $via->save();
  
  $via->grado = $grado;
  $via->accesso = $acceso;
  $via->sector_guid = $sector_guid;
  
  if (add_entity_relationship($via_guid, 'viaPerteneceSector', $sector_guid)){
  		system_message("Via asociada a un sector");
  }
  else {
  	register_error("No se pudo crear la relacion entre sector y via");
    forward(REFERER); // REFERER is a global variable that defines the previous page
  }
  
   if ($via_guid) {
     system_message("Tu via ha sido guardada");
     forward($via->getURL());
  } else {
     register_error("La via no pudo ser guardada");
     forward(REFERER); // REFERER is a global variable that defines the previous page
  }
  